﻿
using System;
using Forums.Domain;
using System.Data;
using System.IO;

namespace Forums.Data
{
	
	public class UserDaoPGImpl : GenericDaoImpl,IUserDao
	{
		
		//Get User - ID
		public User GetUser(int  Id)
		{
			return(User) Execute(Id,new DoWithcommand(GetUserDelegate));
			
			
		}
		public static Object  GetUserDelegate(Object Id, IDbCommand dbcmd)
		{
			string cmdquery = "SELECT FIRST_NAME,LAST_NAME,USER_NAME,USER_PWD,EMAIL,CITY ,COUNTRY,TIMEZONE_CODE FROM Users WHERE ID = @id";
			dbcmd.CommandText = cmdquery;
			
			IDbDataParameter param =dbcmd.CreateParameter();
			param.DbType = DbType.Int16;
			param.ParameterName = "id";
			param.Value = Id;
			dbcmd.Parameters.Add(param);
			User u = null;
			
			IDataReader dr = dbcmd.ExecuteReader();
			
			if (dr.Read())
			{
				u = new User();
				u.FirstName  = dr.GetString(0);
				u.LastName  = dr.GetString (1);
				u.UserName   = dr.GetString (2);
				u.UserPwd   = dr.GetString (3);
				u.Email = dr.GetString(4);
				u.City  = dr.GetString (5);
				u.Country  = dr.GetString (6);
				u.TimezoneCode   = dr.GetString (7);
				u.UserId =Convert.ToInt32(Id);
				return u;
				
			}

			return null;
		}
		
		//Get User By UserName
		public User GetUserByUserName(String UserName){
			return(User) Execute(UserName,new DoWithcommand(GetUserByUserNameDelegate));
		}
		public static Object GetUserByUserNameDelegate(Object o, IDbCommand dbcmd){
			string cmdquery = "SELECT FIRST_NAME,LAST_NAME,ID,USER_PWD,EMAIL,CITY ,COUNTRY,TIMEZONE_CODE FROM Users WHERE USER_NAME = @un";
			dbcmd.CommandText = cmdquery;
			
			String UserName = (String) o;
			
			IDbDataParameter param =dbcmd.CreateParameter();
			param.DbType = DbType.String;
			param.ParameterName = "un";
			param.Value = UserName;
			dbcmd.Parameters.Add(param);
			User u = null;
			
			IDataReader dr = dbcmd.ExecuteReader();
			
			if (dr.Read())
			{
				u = new User();
				u.FirstName  = dr.GetString(0);
				u.LastName  = dr.GetString (1);
				u.UserId   = dr.GetInt32 (2);
				u.UserPwd   = dr.GetString (3);
				u.Email = dr.GetString(4);
				u.City  = dr.GetString (5);
				u.Country  = dr.GetString (6);
				u.TimezoneCode   = dr.GetString (7);
				u.UserName  = UserName;
				
			}

			return u;
		}
		
		//Delete User
		public void  DeleteUser(int Id)
		{
			Execute(Id,new DoWithcommand(DeleteUserDelegate));
		}
		public static Object DeleteUserDelegate(Object Id,IDbCommand dbcmd)
		{
			string cmdquery = "DELETE FROM USERS WHERE ID = @biju";
			dbcmd.CommandText = cmdquery;
			IDbDataParameter param = dbcmd.CreateParameter();
			param.DbType = DbType .Int16;
			param.ParameterName ="biju";
			param.Value = Id;
			dbcmd.Parameters.Add(param);
			dbcmd.ExecuteNonQuery();
			Console.WriteLine("Record deleted");
			return null;
			
		}
		
		
		// save or update
		public void SaveorUpadateUser(User u)
		{
			if(u.UserId > 0){
				Execute(u,new DoWithcommand(UpdateDelegate));
			} else {
				Execute(u,new DoWithcommand(SaveDeletgate));
			}
		}
		public static Object SaveDeletgate(Object o , IDbCommand dbcmd){
			User u = (User)o;
			string cmdquery = ("INSERT INTO USERS(FIRST_NAME,LAST_NAME,USER_NAME,USER_PWD,EMAIL ,CITY ,COUNTRY,TIMEZONE_CODE ) " +
					                   "VALUES (@FirstName,@LastName,@UserName,@UserPwd,@Email,@City,@Country,@TimeZone)");
					AddParameter (dbcmd,"@FirstName",DbType.String,u.FirstName );
					AddParameter (dbcmd,"@LastName",DbType.String,u.LastName );
					AddParameter (dbcmd,"@UserName",DbType.String,u.UserName );
					AddParameter (dbcmd,"@UserPwd",DbType.String,u.UserPwd );
					AddParameter (dbcmd,"@Email",DbType.String,u.Email );
					AddParameter (dbcmd,"@City",DbType.String,u.City );
					AddParameter (dbcmd,"@Country",DbType.String,u.Country );
					AddParameter (dbcmd,"@TimeZone",DbType.String,u.TimezoneCode );
					dbcmd.CommandText = cmdquery;
					dbcmd.ExecuteNonQuery();
					return null;
		}
		public static Object UpdateDelegate(Object o,IDbCommand dbcmd)
		{
			User u = (User)o;
			

				Console.WriteLine("userid is there then its updated  into table");
				dbcmd.CommandText = "UPDATE USERS SET " + 
					"FIRST_NAME = @fn,LAST_NAME =@ln,USER_PWD =@pwd,EMAIL=@em , " + 
					" CITY =@ct ,COUNTRY =@ctry,TIMEZONE_CODE =@tz  " + 
					" WHERE ID =@id";
				
				AddParameter(dbcmd,"@id",DbType.Int16,u.UserId );
				AddParameter (dbcmd,"@fn",DbType.String,u.FirstName );
				AddParameter (dbcmd,"@ln",DbType.String,u.LastName );
				AddParameter (dbcmd,"@pwd",DbType.String,u.UserPwd );
				AddParameter (dbcmd,"@em",DbType.String,u.Email );
				AddParameter (dbcmd,"@ct",DbType.String,u.City );
				AddParameter (dbcmd,"@ctry",DbType.String,u.Country );
				AddParameter (dbcmd,"@tz",DbType.String,u.TimezoneCode );
				dbcmd.ExecuteNonQuery();
				return null;

				
			}
		
	
	}
		
		
}